Working with the Viewer > Customizing the Viewer > Viewer API Overview |
The HTML5 Viewer API permits programmatic control over the Viewer.
Most API functionality is exposed by the ViewerControl - the core component of the Viewer. The viewer UI/chrome builds off of the API members of the ViewerControl.
It is required to use the HTML5 Viewer API for:
The HTML5 Viewer API is not required for:
Functionality that is exposed through the Viewer API includes:
Basic knowledge for getting started using the API is covered below.
The API classes are exposed through the global PCCViewer object.
Example |
Copy Code
|
---|---|
window.PCCViewer; |
A ViewerControl is created when instantiating the Viewer with the jQuery plugin, and it is accessible through the 'viewerControl' property of the returned object.
Example |
Copy Code
|
---|---|
var viewer = $("#mydiv").pccViewer(options); var viewerControl = viewer.viewerControl; |
While initialization of the Viewer is being performed, it is unsafe to call some of the ViewerControl methods (these methods will throw an error during initialization). The "ViewerReady" event signals that initialization has completed and it is safe to call all ViewerControl methods with valid data.
Example |
Copy Code
|
---|---|
var viewerControl = $("#mydiv").pccViewer(options).viewerControl; // Subscribe to the "ViewerReady" event viewerControl.on(PCCViewer.EventType.ViewerReady, function(ev) { // It is now safe to call all ViewerControl methods viewerControl.setCurrentMouseTool("AccusoftLineAnnotation"); // It’s also safe to use ECMA5 properties // (in supporting browsers). var currentPageNumber = viewerControl.pageNumber; // This is also a good time to subscribe to other events, // if you want to keep all API code in one place. viewerControl.on(PCCViewer.EventType.PageDisplayed, ...); }); |
The ViewerControl will not get the page count of a document from Prizm Services until after the "ViewerReady" event. Before the page count is returned, the ViewerControl assumes every document has one page.
The "PageCountReady" event signals when the ViewerControl has the actual page count from the server. Subscribe to this event to know when you can navigate to and interact with other pages in the document.
Example |
Copy Code
|
---|---|
var viewerControl = $("#mydiv").pccViewer(options).viewerControl; // Subscribe to the "PageCountReady" event viewerControl.on(PCCViewer.EventType.PageCountReady, function(ev) { var pageCount = ev.pageCount; // We can now navigate to other pages in the document. if (pageCount > 1) { // Go to the second page of the document. viewerControl.setPageNumber(2); } }); |
It is safe to call viewer navigation API members before the page count is available to the ViewerControl, but navigation outside of the known page count (of one) is not possible. |
Calculating the actual page count of some document formats can take a significant amount of time, and therefore waiting on an actual page count could make the Viewer appear un-responsive to the end user. To address this problem, the Prizm Services may quickly generate an estimated page count.
The "EstimatedPageCountEvent" signals that the Viewer has an estimated page count and that navigation to other pages is possible.
Example |
Copy Code
|
---|---|
var viewerControl = $("#mydiv").pccViewer(options).viewerControl; // Subscribe to the "PageCountReady" event viewerControl.on(PCCViewer.EventType.EstimatedPageCountReady, ...); |
Page numbering in the API starts with 1. This is true for events and methods. The API does not support 0-based page indexes, so be careful when iterating over pages.
Example |
Copy Code
|
---|---|
// Navigate to the first page of the document. viewerControl.changeToFirstPage(); // An equivalent call would be viewerControl.setPageNumber(1); |
In the API, the unit of measure for height, width, location, and other sizes is a pixel. To determine the height and width of a page in pixels, use the ViewerControl’s requestPageAttributes method.
Example |
Copy Code
|
---|---|
// Get attributes of page 1. viewerControl.requestPageAttributes(1).then(function(attributes) { var pageWidth = attributes.width; var pageHeight = attributes.height; // Add a rectangle that is the full width and height of page 1 viewerControl.addMark(1, "RectangleAnnotation") .setRectangle({x:0, y:0, height: pageHeight, width: pageWidth}); }); |
It is advised to check page attributes before getting or setting the location of a Mark object. |
The API commonly uses getter and setter methods for accessing and modifying data associated with objects. Typically these methods are in pairs, unless the data is read-only.
Example |
Copy Code
|
---|---|
// read-write viewerControl.getPageNumber(); // get the current page number viewerControl.setPageNumber(1); // set the current page to 1 // read-only viewerControl.getPageCount(); // get the page count |
The API also offers ECMA5 Accessor properties that correspond to the getter and setters. These accessor properties offer a different style of coding, and they are only available in modern browsers that implement Object.defineProperties.
Example |
Copy Code
|
---|---|
if (Object.defineProperties) { viewerControl.pageNumber; // get the current page number viewerControl.pageNumber = 1; // set the current page to 1 viewerControl.pageCount; // get the page count } |
The API is designed to support method chaining. Most methods will return the current context (object on which the method was called) in order to promote method chaining. Exceptions to this rule are getter methods and methods that complete asynchronously.
Example |
Copy Code
|
---|---|
// Method chaining when creating a rectangle annotation viewerControl.addMark(1, "RectangleAnnotation") .setRectangle({x: 0, y:0, width: 100, height: 100}) .setFillColor("#FFFFFF") .setOpacity(127) .setBorderThickness(6); // Method chaining when manipulating the page viewerControl .rotatePage(90) .zoomIn(1.2); |
Note that using the ECMA5 accessor properties often discourages method chaining. |
The API implements the Promises/A+ spec in the class PCCViewer.Promise.
Many methods that complete asynchronously return a PCCViewer.Promise object. Use the returned promise object to subscribe callbacks for the completion of the asynchronous event.
Example |
Copy Code
|
---|---|
// Use the promise to subscribe a callback to requestPageText viewerControl.requestPageText(1).then(function(pageText) { alert("Text of page 1 is: " + pageText); }); |
Two exceptions are ViewerControl#print and ViewerControl#search, which complete asynchronously but do not return a PCCViewer.Promise object. These methods both return objects that represent the requested print and search. Searching and printing have status events and are cancellable, for which they cannot be suitably represented with a promise object.
To learn about the various areas of API functionality, examples and an API reference are provided: